/**
 * Created by Bo on 2016/12/2.
 */



(function ($) {

    var htmltemplate = ' <div id="controlTemplate">' +
        '<div class="col-lg-2 col-sm-2 col-md-2 col-xs-12">' +
        '<div class="checkbox">' +
        '<label style="color:#222;">' +
        '<input class="colored-success" type="checkbox">' +
        '<span class="text">checkbox</span>' +
        '</label>' +
        '</div>' +
        '</div>' +
        '<div class="col-lg-2 col-sm-2 col-md-2 col-xs-12">' +
        '<div class="checkbox">' +
        '<label style="color:#222;">' +
        '<input class="colored-success" type="radio">' +
        '<span class="text">radio</span>' +
        '</label>' +
        '</div>' +
        '</div>' +
        '</div>';

    var checkboxpicker = function (element, setting) {
        element.html('');
        var controltemplate = $(htmltemplate).children().eq(setting.type);
        if (setting.url) {
            var parm = typeof (setting.param) == "function" ? setting.param() : setting.param;
            $.request(setting.url, parm, function (result) {
                bind(result);
                if (setting.callback) {
                    setting.callback();
                }
            }, false, false);
        } else bind(setting.data);


        function bind(data) {
            if (data && data.length > 0) {
                $.each(data, function (i, val) {
                    var control = controltemplate.clone().attr("name", setting.name);
                    control.find("span").html(val[setting.value]).attr("value", val[setting.key]);
                    element.append(control);
                });
            }

        }

    }
    $.fn.checkboxpicker = function (setting) {
        new checkboxpicker($(this), $.extend(true, {}, defaults, setting));
    }

    var defaults = {
        url: '/common/dictionary/query/parentId',
        param: {token: context.token},
        key: 'id',
        value: "name",
        name: 'name',
        type: 0,
        data: [],
        callback: undefined,
    };

})(jQuery)